package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.domain.StockReq;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.StockReqLineUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.StockReqLineService;
import com.fan.shop.services.StockReqService;
import com.fan.shop.services.StoreService;
import com.fan.shop.services.TransactionReferenceService;
import com.fan.shop.services.UserService;

public class StockReqCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(StockReqCreateProcess.class);
	private String requisitionNo;
	private String officerCode;
	private String storeCode;
	private Date transactionDate;
	private String transactionCode;
	private String status;
	private BigDecimal postage;
	private String comment;

	private StockReqService stockReqService;
	private StockReqLineService stockReqLineService;
	private StoreService storeService;
	private UserService userService;

	private List<StockReqLineUserClass> attachedLines;

	private TransactionReferenceService transactionReferenceService;

	public String getRequisitionNo() {
		return requisitionNo;
	}

	public void setRequisitionNo(String requisitionNo) {
		this.requisitionNo = requisitionNo;
	}

	public String getOfficerCode() {
		return officerCode;
	}

	public void setOfficerCode(String officerCode) {
		this.officerCode = officerCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public Date getTransactionDate() {
		return transactionDate;
	}

	public void setTransactionDate(Date transactionDate) {
		this.transactionDate = transactionDate;
	}

	public String getTransactionCode() {
		return transactionCode;
	}

	public void setTransactionCode(String transactionCode) {
		this.transactionCode = transactionCode;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public BigDecimal getPostage() {
		return postage;
	}

	public void setPostage(BigDecimal postage) {
		this.postage = postage;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}

	public StockReqLineService getStockReqLineService() {
		return stockReqLineService;
	}

	public void setStockReqLineService(StockReqLineService stockReqLineService) {
		this.stockReqLineService = stockReqLineService;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<StockReqLineUserClass> getAttachedLines() {
		return attachedLines;
	}

	public void setAttachedLines(List<StockReqLineUserClass> attachedLines) {
		this.attachedLines = attachedLines;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		StockReqCreateProcess.logger = logger;
	}

	@Override
	public Integer processImpl() {
		if (stockReqService.hasStockReq(requisitionNo)) {
			setErrorMessage("Stock Requisition: " + requisitionNo
					+ " already exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store: " + storeCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!userService.hasUser(officerCode)) {
			setErrorMessage("User: " + officerCode + " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}
		if (!transactionReferenceService
				.hasTransactionReference(transactionCode)) {
			setErrorMessage("TransactionCode: " + transactionCode
					+ " doesn't exists!");
			return ShopGlobalVariables.FAILED;
		}

		NewNumberGenerationProcess newNumberGenerationProcess = new NewNumberGenerationProcess();
		newNumberGenerationProcess
				.setTransactionReferenceService(transactionReferenceService);
		newNumberGenerationProcess.setTransactionCode(transactionCode);
		int result = newNumberGenerationProcess.process();
		if (result == ShopGlobalVariables.FAILED) {
			setErrorMessage("Error when creating requisition no!");
			return ShopGlobalVariables.FAILED;
		}
		requisitionNo = newNumberGenerationProcess.getNewNumber();

		StockReq stockReq = new StockReq();
		stockReq.setRequisitionNo(requisitionNo);
		stockReq.setOfficerCode(officerCode);
		stockReq.setStoreCode(storeCode);
		stockReq.setComment(comment);
		stockReq.setPostage(postage);
		stockReq.setStatus(status);
		stockReq.setTransactionCode(transactionCode);
		transactionDate = new Date(System.currentTimeMillis());
		stockReq.setTransactionDate(transactionDate);
		
		for(StockReqLineUserClass lineUserClass : attachedLines)
		{
			StockReqLineCreateProcess stockReqLineCreateProcess = new StockReqLineCreateProcess();
			stockReqLineCreateProcess.setRequisitionNo(requisitionNo);
			stockReqLineCreateProcess.setItemCode(lineUserClass.getItemCode());
			stockReqLineCreateProcess.setLineStatus(status);
			stockReqLineCreateProcess.setOfficerCode(lineUserClass.getOfficerCode());
			stockReqLineCreateProcess.setPrice(lineUserClass.getPrice());
			stockReqLineCreateProcess.setQty(lineUserClass.getQty());
			stockReqLineCreateProcess.setStockReqLineService(stockReqLineService);
			stockReqLineCreateProcess.setStoreCode(lineUserClass.getStoreCode());
			stockReqLineCreateProcess.setStoreService(storeService);
			stockReqLineCreateProcess.setTransactionDate(lineUserClass.getTransactionDate());
			stockReqLineCreateProcess.setUserService(userService);
			
			if(lineUserClass.getStoreCode().length() == 0)
			{
				stockReqLineCreateProcess.setStoreCode(storeCode);
			}
			if(lineUserClass.getOfficerCode().length() == 0)
			{
				stockReqLineCreateProcess.setOfficerCode(officerCode);
			}
			
			int res = stockReqLineCreateProcess.process();
			if(res == ShopGlobalVariables.FAILED)
			{
				return ShopGlobalVariables.FAILED;
			}
		}
		
		stockReqService.addStockReq(stockReq);

		return ShopGlobalVariables.SUCCESS;
	}
	
	

}
